package com.acme.chat.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class MyConnect {
	private Connection con = null;
	private String url;
	private String user;
	private String password;
	private Statement stat=null;
	public void startConnection() {
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			con = DriverManager.getConnection(url, user, password);
			if (!con.isClosed()) {
				System.out.println("Connected to database.");
			}
			stat=con.createStatement();
		} catch (SQLException e) {
			System.out.println("ERROR");
		} catch (InstantiationException e) {
			System.out.println("Can't get instance!");
		} catch (IllegalAccessException e) {
			System.out.println("Can't get connection!");
		} catch (ClassNotFoundException e) {
			System.out.println("Class Draver not found!");
		}
	}

	public MyConnect(String url, String port, String nameDB, String user,
			String password) {
		this.url = "jdbc:mysql://" + url + ":" + port + "/" + nameDB;
		this.user = user;
		this.password = password;
	}

	public void closeConnection() {
		try {
			System.out.println("Close connection.");
			stat.close();
			con.close();
		} catch (SQLException e) {
			System.out.println("Try close connection....\n");
			e.printStackTrace();
		}
	}

	public Statement getStatement() {
		return stat;
	}
}

